#!groovy jenkins配置
pipeline{
    agent any
    //定义仓库地址
    environment {
        REPOSITORY="https://gitee.com/lesserPanda/spring-cloud-alibaba-parent-demo.git"
    }

    stages {

        stage('获取代码'){
            steps {
                echo "start fetch code from git:${REPOSITORY}"
                //清空当前目录
                deleteDir()
                //拉去代码
                git "${REPOSITORY}"
            }
        }

        stage('代码静态检查'){
            steps {
                //伪代码检查
                echo "start code check"
            }
        }

        stage('编译+单元测试'){
            steps {
                echo "start compile"
                //切换目录
                dir('../spring-cloud-alibaba-parent-demo') {
                    //重新打包
                    bat 'mvn clean package install -U'
                }
            }
        }

        stage('构建镜像') {
            steps {
                echo "start build image"
                //登录镜像仓库
                bat 'docker login -u *** -p ***'
                // 打包镜像并推送镜像仓库
                dir('../spring-cloud-alibaba-parent-demo/spring-cloud-alibaba-gateway') {
                    //build镜像，mvn命令打包镜像
                    bat 'mvn dockerfile:build'
                    //推送镜像到镜像仓库
                    //bat 'docker push manbanpai/spring-cloud-alibaba-gateway:0.0.1-SNAPSHOT'
                }
                dir('../spring-cloud-alibaba-parent-demo/spring-cloud-alibaba-config') {
                    //build镜像，mvn命令打包镜像
                    bat 'mvn dockerfile:build'
                    //推送镜像到镜像仓库
                    //bat 'docker push manbanpai/spring-cloud-alibaba-config:0.0.1-SNAPSHOT'
                }
                dir('../spring-cloud-alibaba-parent-demo/spring-cloud-alibaba-provider') {
                    //build镜像，mvn命令打包镜像
                    bat 'mvn dockerfile:build'
                    //推送镜像到镜像仓库
                    //bat 'docker push manbanpai/spring-cloud-alibaba-provider:0.0.1-SNAPSHOT'
                }
                dir('../spring-cloud-alibaba-parent-demo/spring-cloud-alibaba-consumer') {
                    //build镜像，mvn命令打包镜像
                    bat 'mvn dockerfile:build'
                    //推送镜像到镜像仓库
                    //bat 'docker push manbanpai/spring-cloud-alibaba-consumer:0.0.1-SNAPSHOT'
                }
            }
        }

        stage('启动服务'){
            steps {
                echo "start spring-cloud-alibaba-parent-demo"
                dir('../spring-cloud-alibaba-parent-demo'){
                    //重启服务docker-compose方式
                    //bat 'docker-compose up -d --build'
                    // 重启服务docker方式
                    bat 'docker run -d --name spring-cloud-alibaba-gateway-0.0.1-SNAPSHOT --network host -v spring-cloud-alibaba-gateway-0.0.1-SNAPSHOT-logs:/logs -e SPRING_PROFILES_ACTIVE=dev manbanpai/spring-cloud-alibaba-gateway:0.0.1-SNAPSHOT'
                    bat 'docker run -d --name spring-cloud-alibaba-config-0.0.1-SNAPSHOT --network host -v spring-cloud-alibaba-config-0.0.1-SNAPSHOT-logs:/logs -e SPRING_PROFILES_ACTIVE=dev manbanpai/spring-cloud-alibaba-config:0.0.1-SNAPSHOT'
                    bat 'docker run -d --name spring-cloud-alibaba-provider-0.0.1-SNAPSHOT --network host -v spring-cloud-alibaba-provider-0.0.1-SNAPSHOT-logs:/logs -e SPRING_PROFILES_ACTIVE=dev manbanpai/spring-cloud-alibaba-provider:0.0.1-SNAPSHOT'
                    bat 'docker run -d --name spring-cloud-alibaba-consumer-0.0.1-SNAPSHOT --network host -v spring-cloud-alibaba-consumer-0.0.1-SNAPSHOT-logs:/logs -e SPRING_PROFILES_ACTIVE=dev manbanpai/spring-cloud-alibaba-consumer:0.0.1-SNAPSHOT'
                }
            }
        }

    }
}